package com.phonemap.service.server;

import com.phonemap.service.server.jaxws.CreateOrUpdatePosition;
import com.phonemap.service.server.jaxws.CreateOrUpdatePositionResponse;
import com.phonemap.service.server.jaxws.CreateOrUpdateUser;
import com.phonemap.service.server.jaxws.CreateOrUpdateUserResponse;
import com.phonemap.service.server.jaxws.DeletePosition;
import com.phonemap.service.server.jaxws.DeletePositionResponse;
import com.phonemap.service.server.jaxws.DeleteUser;
import com.phonemap.service.server.jaxws.DeleteUserResponse;
import com.phonemap.service.server.jaxws.GetAllPositions;
import com.phonemap.service.server.jaxws.GetAllPositionsResponse;
import com.phonemap.service.server.jaxws.GetAllUsers;
import com.phonemap.service.server.jaxws.GetAllUsersResponse;
import com.phonemap.service.server.jaxws.GetPosition;
import com.phonemap.service.server.jaxws.GetPositionResponse;
import com.phonemap.service.server.jaxws.GetPositionsForUser;
import com.phonemap.service.server.jaxws.GetPositionsForUserResponse;
import com.phonemap.service.server.jaxws.GetUser;
import com.phonemap.service.server.jaxws.GetUserResponse;

public class PhonemapAdapter {

	private PhonemapAPI phonemapAPI = new PhonemapAPI();
	
	/**
	 * Generate the Response Object corresponding to the argument in the request
	 * object
	 * 
	 * @param request
	 * @return returns response object
	 */

	public CreateOrUpdateUserResponse createOrUpdateUser(
			CreateOrUpdateUser request) {
		String phone = request.getPhone();
		String firstname = request.getFirstname();
		String lastname = request.getLastname();
		String email = request.getEmail();
		String password = request.getPassword();
		String avatar = request.getAvatar();
		boolean status = phonemapAPI.createOrUpdateUser(phone, firstname,
				lastname, email, password, avatar);
		CreateOrUpdateUserResponse response = new CreateOrUpdateUserResponse();
		response.setReturn(status);
		return response;
	}

	public CreateOrUpdatePositionResponse createOrUpdatePosition(
			CreateOrUpdatePosition request) {
		CreateOrUpdatePositionResponse response = new CreateOrUpdatePositionResponse();
		try {
			String userId = request.getUserId();
			Double latitude = Double.parseDouble(request.getLatitude()
					.toString());
			Double longitude = Double.parseDouble(request.getLongitude()
					.toString());
			boolean status = phonemapAPI.createOrUpdatePosition(userId, latitude,
					longitude);
			response.setReturn(status);
			return response;
		} catch (Exception e) {
			e.printStackTrace();
		}
		response.setReturn(false);
		return response;
	}

	public DeleteUserResponse deleteUser(DeleteUser request) {
		String phone = request.getPhone();
		String status = phonemapAPI.deleteUser(phone);
		DeleteUserResponse response = new DeleteUserResponse();
		response.setReturn(status);
		return response;
	}

	public DeletePositionResponse deletePosition(DeletePosition request) {
		String userId = request.getUserId();
		String status = phonemapAPI.deletePosition(userId);
		DeletePositionResponse response = new DeletePositionResponse();
		response.setReturn(status);
		return response;
	}

	public GetUserResponse getUser(GetUser request) {
		String phone = request.getPhone();
		String productJSON = phonemapAPI.getUser(phone);
		GetUserResponse response = new GetUserResponse();
		response.setReturn(productJSON);
		return response;
	}

	public GetAllUsersResponse getAllUsers(GetAllUsers request) {
		String kind = request.getKind();
		String productsJSON = phonemapAPI.getAllUsers(kind);
		GetAllUsersResponse response = new GetAllUsersResponse();
		response.setReturn(productsJSON);
		return response;
	}

	public GetPositionResponse getPosition(GetPosition request) {
		String position = request.getUserId();
		String itemJSON = phonemapAPI.getPosition(position);
		GetPositionResponse response = new GetPositionResponse();
		response.setReturn(itemJSON);
		return response;
	}

	public GetPositionsForUserResponse getPositionsForUser(
			GetPositionsForUser request) {
		String kind = request.getKind();
		String userId = request.getUserId();
		String itemsJSON = phonemapAPI.getPositionsForUser(kind, userId);
		GetPositionsForUserResponse response = new GetPositionsForUserResponse();
		response.setReturn(itemsJSON);
		return response;
	}

	public GetAllPositionsResponse getAllPositions(GetAllPositions request) {
		String kind = request.getKind();
		String itemsJSON = phonemapAPI.getAllPositions(kind);
		GetAllPositionsResponse response = new GetAllPositionsResponse();
		response.setReturn(itemsJSON);
		return response;
	}
}
